﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.ClientDisplayMechanics {
    import flash.events.*;
    import flash.geom.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import flash.utils.*;
    import Classes.*;
    import validation.*;

    public class NotifyGuideMechanic implements IClientGameMechanic, IValidationMechanic {

        private static var m_eventTriggers:Dictionary = null;

        protected var m_owner:MechanicMapResource;
        protected var m_type:String;
        protected var m_validation:GenericValidationScript;
        protected var m_triggerName:String;
        protected var m_event:String;
        protected var m_priority:int;

        private static function register(_arg1:String, _arg2:String, _arg3:int, _arg4:NotifyGuideMechanic):void{
            var _local5:Object = {
                name:_arg2,
                priority:_arg3,
                mechanic:_arg4
            };
            if (!m_eventTriggers){
                m_eventTriggers = new Dictionary();
                Global.world.addEventListener(_arg1, onTriggerEvent, false, 0, true);
            };
            if (!m_eventTriggers[_arg1]){
                m_eventTriggers[_arg1] = new Array();
            };
            var _local6:Boolean;
            var _local7:int;
            while (_local7 < m_eventTriggers[_arg1].length) {
                if (m_eventTriggers[_arg1][_local7].priority > _arg3){
                    m_eventTriggers[_arg1].splice(_local7, 0, _local5);
                    _local6 = true;
                };
                _local7++;
            };
            if (!_local6){
                m_eventTriggers[_arg1].push(_local5);
            };
        }
        private static function buildEntry(_arg1:String, _arg2:int, _arg3:NotifyGuideMechanic):Object{
            return ({
                name:_arg1,
                priority:_arg2,
                mechanic:_arg3
            });
        }
        private static function onTriggerEvent(_arg1:Event):void{
            dequeueTrigger(_arg1.type);
        }
        public static function onGuideComplete(_arg1:XMLList):void{
            var _local2:String = String(_arg1.@event);
            if (((_local2) && ((_local2.length > 0)))){
                dequeueTrigger(_local2);
            };
        }
        private static function dequeueTrigger(_arg1:String):void{
            var _local3:Boolean;
            var _local4:int;
            var _local2:Array = m_eventTriggers[_arg1];
            if (_local2){
                _local3 = false;
                _local4 = 0;
                while (_local4 < _local2.length) {
                    if (_local2[_local4].mechanic.isValid()){
                        Global.guide.notify(_local2[_local4].name);
                        _local3 = true;
                        break;
                    };
                    _local4++;
                };
                if (_local3){
                    m_eventTriggers[_arg1] = _local2.splice(_local4, 1);
                };
            };
        }

        public function updateDisplayObject(_arg1:Number):void{
        }
        public function isPixelInside(_arg1:Point):Boolean{
            return (false);
        }
        public function initialize(_arg1:IMechanicUser, _arg2:MechanicConfigData):void{
            this.m_owner = (_arg1 as MechanicMapResource);
            this.m_type = _arg2.params["type"];
            this.m_event = _arg2.params["event"];
            this.m_triggerName = _arg2.params["triggerName"];
            this.m_priority = _arg2.params["priority"];
            if (_arg2.params["validate"]){
                this.m_validation = this.m_owner.getItem().getValidation(_arg2.params["validate"]);
            };
            register(this.m_event, this.m_triggerName, this.m_priority, this);
        }
        public function isVisitEnabled():Boolean{
            return (true);
        }
        public function isValid():Boolean{
            var _local1:Boolean;
            if (this.m_validation){
                _local1 = this.m_validation.validate(this.m_owner);
            };
            return (_local1);
        }

    }
}//package Mechanics.ClientDisplayMechanics 
